package Servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import Bean.Car;
import Bean.UserAndCar;
import ServiceImpl.CarServiceImpl;
import ServiceImpl.UserCarServiceImpl;

/**
 * Servlet implementation class SelectCarServlet
 */
public class SelectCarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
    public SelectCarServlet() {}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		CarServiceImpl carServiceImpl = new CarServiceImpl();
		UserCarServiceImpl userCarServiceImpl = new UserCarServiceImpl();
		UserAndCar car = new UserAndCar();
		UserAndCar userAndCar = new UserAndCar();
		List<UserAndCar> cars = null;
		response.setContentType("text/html charset = UTF-8");
		String selectType = request.getParameter("selectType");
		String selectText = request.getParameter("selectText");
		switch(selectType) {
		case "1": // 根据车牌号查询
			cars = new ArrayList<UserAndCar>();
			cars.add(userCarServiceImpl.selectOneUserAndCarByCarId(selectText));
			//System.out.println(cars);
			request.setAttribute("Result", cars);
			break;
		case "2": // 根据车主查询
			cars = userCarServiceImpl.selectAllCarOfUser(selectText);
			System.out.print(cars);
			request.setAttribute("Result", cars);
			break;
		case "3": //  根据创建日期查询
			cars = userCarServiceImpl.selectAllUserAndCarByCreateTime(selectText);
			System.out.print(cars);
			request.setAttribute("Result", cars);
			break;
		default:
			request.setAttribute("Result", null);
			break;
		}
		request.setAttribute("isOk", true);
		request.getRequestDispatcher("select_car_info.jsp").forward(request, response);
	}

}
